package com.tianqu.tms.www.transfer.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.www.transfer.controller.CarStockAcceptTransferInfoController;

/**
 * 库存车辆受理-更新数据 action
 * @author mufeiyu
 *
 */
public class CarStockAcceptTransferInfoAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarStockAcceptTransferInfoAction.class);
	public static final String SERIAL = "serial";
	public static final String OWNERNAME = "ownerName";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");

		CarStockAcceptTransferInfoController controller = new CarStockAcceptTransferInfoController();
		try {
			controller.execute(uc);
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		String checkSerial = (String)uc.getAttribute("checkSerial");
		uc.removeAttribute("checkSerial");
		TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
		transferAcceptedInfo = transferAcceptedInfo.getAcceptInfoBySerial(checkSerial,"");
		String lipVi = "";// 车牌号码/VIN码
		String newOwner = "";
		String oldOwner = "";
		String ownerName = transferAcceptedInfo.getCarOwner();
		TransferFinanceInfo finance = new TransferFinanceInfo();
		finance = finance.findFinanceInfoByserial(checkSerial);
		int checkPrice = finance.getCheckPrice();
		if (transferAcceptedInfo.getCarClass()== 1) {
			lipVi = transferAcceptedInfo.getVin();
			newOwner = ownerName;
		} else {
			lipVi = transferAcceptedInfo.getLicensePlate();
			oldOwner = ownerName;
		}
		String background = "";
		if("1".equals(Registry.getConfig(TmsConstants.WWW_LIUCHENG_HOST_KEY)))
		{
			background = "<img class='jp-paper-background' src='style/images/memuD.jpg' style='WIDTH:740px; HEIGHT:959px'/>";
		}else{
			background = "<img class='jp-paper-background' src='style/images/menuQ.jpg'  style='WIDTH: 766px; HEIGHT: 1083px'/>";
		}
		// 拼select
		String barPic = createBarcode(request,checkSerial);
		StringBuffer str = new StringBuffer();
		str.append(""
						+ ""+background+""
						+ "<div class='jp-label jp-comp-41'>" + lipVi
						+ "</div>" + "<div class='jp-label jp-comp-42'>"
						+ checkSerial + "</div>"
						+ "<div class='jp-label jp-comp-43'>" + oldOwner + "</div>" +
						"<div class='jp-label jp-comp-45'>"+checkSerial+"</div>" +
						"<div class='jp-label jp-comp-46'>"+transferAcceptedInfo.getLicensePlate()+"</div>" +
						"<div class='jp-label jp-comp-47'>"+ownerName+"</div>" +
						"<div class='jp-label jp-comp-48'>"+DateUtil.getCurrentTimeStr3()+"</div>" +
						"<div class='jp-label jp-comp-51'>"+oldOwner +"</div>" + //WL:20160509
						"<div class='jp-label jp-comp-52'>"+checkSerial +"</div>" + //WL:20160509
						"<div class='jp-label jp-comp-53'>"+transferAcceptedInfo.getLicensePlate() +"</div>" +
						"<div class='jp-label jp-comp-54'>"+checkSerial +"</div>" + //WL:20160509
						"<div class='jp-label jp-comp-55'>"+transferAcceptedInfo.getLicensePlate() +"</div>" +
						//barPic
						"<div class='jp-label jp-comp-56'><img src='"+barPic+"'/></div>" +
						"<div class='jp-label jp-comp-57'><img src='"+barPic+"'/></div>" +
						"<div class='jp-label jp-comp-58'><img src='"+barPic+"'/></div>" +
						"<div class='jp-label jp-comp-59'><img src='"+barPic+"'/></div>" +
						"<div class='jp-label jp-comp-60'><img src='"+barPic+"'/></div>" +
						
						"<div class='jp-label jp-comp-33'>"+transferAcceptedInfo.getLicensePlate()+"</div>" +
						"<div class='jp-label jp-comp-34'>"+transferAcceptedInfo.getSerial()+"</div>" +
						"<div class='jp-label jp-comp-35'>"+checkPrice+"</div>");
		String responseText = str.toString();
		
		uc.setAttribute("lcText", responseText);
		uc.setAttribute("lcPrintFlag", "1");

		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}	
	private String createBarcode(HttpServletRequest request,String key){
		try {
			JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance());
			localJBarcode.setEncoder(Code39Encoder.getInstance());
			localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
			localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
			localJBarcode.setShowCheckDigit(false);
			String paramString = key + ".jpg";
			BufferedImage localBufferedImage = localJBarcode.createBarcode(key);
			String path = request.getSession().getServletContext().getRealPath("/").replace("/tmsweb", "") + Registry.getConfig(TmsConstants.WWW_UPLOAD_TIAO_UPLOAD_KEY);
			File file = new File(path);
			if (!file.exists()) file.mkdirs();
			FileOutputStream localFileOutputStream = new FileOutputStream(path + "/" + paramString);
			ImageUtil.encodeAndWrite(localBufferedImage, "jpeg", localFileOutputStream, 96, 96);
			localFileOutputStream.close();
			return Registry.getConfig(TmsConstants.WWW_UPLOAD_TIAO_UPLOAD_KEY) + "/" + paramString;
		} catch (Exception e) {
			logger.error("createBarcode error", e);
		}
		return "";
	}
}